//
//  RAPMainInterfaceTabBarController.swift
//  COPYCAT-Weibo
//
//  Created by 闫萌 on 12/27/15.
//  Copyright © 2015 RendezvousAuParadis. All rights reserved.
//

import UIKit

class RAPMainInterfaceTabBarController: UITabBarController {
    // MARK: - 系统方法
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 替换tabbar
        let composeButtonTabBar = RAPMainInterfaceTabBar(frame: tabBar.frame)
        composeButtonTabBar.midButtonClickedCallBack = { [weak self] in
            // TODO: 注意这里的循环引用
            RAPLog("微博信息编辑按钮已点击")
            
            let composeMenuView = RAPComposeMenuView(frame: (self?.view.bounds)!)
            composeMenuView.hostController = self
            self?.view.addSubview(composeMenuView)
        }
        
        
        setValue(composeButtonTabBar, forKey: "tabBar")
        
        // 添加子控制器
        addChildViewControllers()
        
    }
    
    
    // MARK: - 自定义方法
    func addChildViewControllers() {
        // home
        addChildViewController(RAPHomeTableViewController(), title: "主页", imageName: "tabbar_home")

        // messgae
        addChildViewController(RAPMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        // discover
        addChildViewController(RAPDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        
        // profile
        addChildViewController(RAPProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    

    func addChildViewController(childController: UIViewController, title: String, imageName: String) {
        /*
        使用自定义tabBarItem类
        */
        childController.tabBarItem = RAPBadgeTabBarItem()
        
        childController.title = title
        childController.tabBarItem.title = childController.title
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFontOfSize(11), NSForegroundColorAttributeName : UIColor.darkGrayColor()], forState: .Normal)
        
        // 普通状态的图片
        var img = UIImage(named: imageName)
        img = img?.imageWithRenderingMode(.AlwaysOriginal)
        // 选中状态的图片
        var sltImg = UIImage(named: "\(imageName)_selected")
        sltImg = sltImg?.imageWithRenderingMode(.AlwaysOriginal)
        // 添加图片
        childController.tabBarItem.image = img
        childController.tabBarItem.selectedImage = sltImg
        
        // 包装一个导航控制器
        let nav = RAPMainNavigationController(rootViewController: childController)
        
        // 添加子控制器
        addChildViewController(nav)
    }
    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
